package com.ky.client.service;

import java.util.Scanner;

import com.ky.client.util.RequestUtil;
import com.ky.common.dto.Dto;
import com.ky.common.entity.KyUser;
import com.ky.common.util.KeyConstants;

public class RegisterService implements Service {

	@Override
	public Service service() {

		Service nextService=null;
		Scanner scanner = new Scanner(System.in);
		while(true) {
			System.out.print("请输入注册的用户名：");
			String userName=scanner.next();
			System.out.print("请输入密码：");
			String userPwd=scanner.next();
			System.out.print("请确认密码：");
			String pwd=scanner.next();
			if(!userPwd.equals(pwd)) {
				System.out.println("两次输入的密码不一致，请重新注册！");
				continue;
			}
			//向服务器端发起请求
			RequestUtil<KyUser> requestUtil = new RequestUtil<>();
			Dto<KyUser> requestDto = new Dto<>();
			//客户端向服务器发起请求时携带的数据
			requestDto.setKey(KeyConstants.REGISTER);
			requestDto.setData(new KyUser(userName, userPwd));
			//通过调用RequestUtil中的request方法，向服务器端发起请求
			Dto<Object> responseDto = requestUtil.request(requestDto);
			if(responseDto.getCode()==200) {
				System.out.println("注册成功，即将进入登录页面。。。。。。");
				nextService=new LoginService();
			}else {
				System.out.println("注册失败，原因："+responseDto.getErrorMsg());
			}
			System.out.print("继续操作请输入y，返回开始页则输入n。请输入：");
			String choice=scanner.next();
			if("n".equals(choice)) {
				nextService=new StartService();
			}
			break;
		}
		return nextService;
	}

}
